//
//  ChatBarView.swift
//  EyeVisionForDoctors
//
//  Created by PMC on 2020/2/7.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit
import SnapKit

let kChatBarViewOriginalHeight: CGFloat = 50.0
let kChatBarViewPadding: CGFloat = 7.0
let kChatBarTextViewHeight: CGFloat = kChatBarViewOriginalHeight - kChatBarViewPadding * 2
let kChatBarTextViewMaxHeight: CGFloat = 120.0
let kChatBarViewToolWidth: CGFloat = 35.0

protocol ChatBarViewDelegate: NSObjectProtocol {
    /// 发送图片
    func chatBarView(didSendImage barView: ChatBarView)
    
    /// 发送视频通话
    func chatBarView(didSendVideoPhone barView: ChatBarView)
}

enum ChatInputType: Int {
    case inputText
    case inputRecord
}

class ChatBarView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    weak var delegate: ChatBarViewDelegate?
    var inputTextViewCurrentHeight = kChatBarViewOriginalHeight
    var inputType: ChatInputType = .inputText {
        didSet {
            inputTextView.isHidden = inputType == .inputRecord
            recordBtn.isSelected = inputType == .inputRecord
        }
    }
    
    lazy var inputTextView: UITextView = { [unowned self] in
        let inputV = UITextView()
        inputV.font = UIFont.systemFont(ofSize: 15.0)
        inputV.textColor = UIColor.black
        inputV.scrollsToTop = false
        inputV.returnKeyType = .send
        inputV.enablesReturnKeyAutomatically = true
        inputV.textContainerInset = UIEdgeInsets(top: 7, left: 5, bottom: 5, right: 5)
        inputV.backgroundColor = UIColor(hex: "#F4F4F4")
        inputV.layoutManager.allowsNonContiguousLayout = false
        inputV.layer.cornerRadius = 5.0
        inputV.layer.masksToBounds = true
        inputV.layer.borderColor = UIColor.splitLineLight.cgColor
        inputV.layer.borderWidth = 1
        return inputV
    }()
    
    private lazy var imgButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "chat_image"), for: .normal)
        button.setBackgroundImage(UIImage(color: .splLineNormal), for: .highlighted)
        button.addTarget(self, action: #selector(imgBtnClick(_:)), for: .touchUpInside)
        return button
    }()
    
    private lazy var videoButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "chat_video"), for: .normal)
        button.setBackgroundImage(UIImage(color: .splLineNormal), for: .highlighted)
        button.addTarget(self, action: #selector(videoBtnClick(_:)), for: .touchUpInside)
        return button
    }()
    
    private lazy var topLine: UIView = {
        let line = UIView()
        line.backgroundColor = .splitLineLight
        return line
    }()
    
    private lazy var bottomLine: UIView = {
        let line = UIView()
        line.backgroundColor = .splitLineLight
        return line
    }()
    
    private lazy var recordBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "chat_type_record"), for: .normal)
        btn.setImage(UIImage(named: "chat_type_text"), for: .selected)
        btn.addTarget(self, action: #selector(recordBtnClick(_:)), for: .touchUpInside)
        return btn
    }()
    
    lazy var speakView: ChatSpeakView = {
        let speakView = ChatSpeakView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
        return speakView
    }()
}

extension ChatBarView {
    private func setupUI() {
        backgroundColor = UIColor.chatKeyboardBg
//        addSubview(videoButton)
        addSubview(imgButton)
        addSubview(recordBtn)
        addSubview(speakView)
        addSubview(inputTextView)
        addSubview(topLine)
        addSubview(bottomLine)
        
        setupUILayout()
    }
    
    private func setupUILayout() {
        topLine.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        recordBtn.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.width.equalTo(22)
            make.top.equalTo(kChatBarViewPadding)
            make.bottom.equalTo(-kChatBarViewPadding)
        }
        
        inputTextView.snp.makeConstraints { (make) in
            make.left.equalTo(recordBtn.snp.right).offset(15)
            make.top.equalTo(kChatBarViewPadding)
            make.right.equalTo(imgButton.snp.left).offset(-16)
            make.bottom.equalTo(-kChatBarViewPadding)
        }
        
        speakView.snp.makeConstraints { (make) in
            make.top.bottom.left.right.equalTo(inputTextView)
        }
        
        imgButton.snp.makeConstraints { (make) in
//            make.right.equalTo(videoButton.snp.left).offset(-kChatBarViewPadding)
            make.right.equalToSuperview().offset(-16)
            make.width.height.equalTo(kChatBarViewToolWidth)
            make.bottom.equalTo(inputTextView)
        }
        
//        videoButton.snp.makeConstraints { (make) in
//            make.right.equalTo(-kChatBarViewPadding)
//            make.width.height.equalTo(kChatBarViewToolWidth)
//            make.bottom.equalTo(imgButton)
//        }
        
        bottomLine.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
}

extension ChatBarView {
    @objc private func imgBtnClick(_ sender: UIButton) {
        inputTextView.resignFirstResponder()
        delegate?.chatBarView(didSendImage: self)
    }
    
    @objc private func videoBtnClick(_ sender: UIButton) {
        inputTextView.resignFirstResponder()
        delegate?.chatBarView(didSendVideoPhone: self)
    }
    
    @objc private func recordBtnClick(_ sender: UIButton) {
        if inputType == .inputText {
            RecordManager.sharedInstance.requestAuthorizations { (sucess) in
                if sucess {
                    self.inputType = .inputRecord
                    self.inputTextView.resignFirstResponder()
                } else {
                    AlertTool.showAlert(mainWindow.rootViewController!,
                                        message: "需要访问您的麦克风\n请启用麦克风-设置-隐私-麦克风",
                                        sureTitle: "去打开",
                                        cancelTitle: "取消",
                                        type: .double,
                                        sureHandler: { (action) in
                        SettingTools.openAppSetting()
                    })
                }
            }
        } else {
            inputType = .inputText
            inputTextView.becomeFirstResponder()
        }
    }
}
